package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @description: 检查组的web层
 * @author: wangjingliang
 * @create: 2019-11-08 14:38
 **/
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * url:/checkgroup/add.do?
     *
     * @param:
     * @return: null
     * @date: 2019/11/8
     */
    @PostMapping("/add")
    public Result add(int[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        int add;

        try {
            add = checkGroupService.add(checkitemIds, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL, e.getMessage());
        }

        if (add == 0) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL, MessageConstant.EXCEPION);
        }

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 根据条件进行分页查询
     *
     * @param: QueryPageBean
     * @return: null
     * @date: 2019/11/6
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = null;
        try {
            pageResult = checkGroupService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, MessageConstant.EXCEPION);
        }


    }


    /**
     * 根据id进行查询
     *url://checkgroup/findCheckGroudAndCheckItemInfo.do?id
     * @param:Integer id
     * @return: List<CheckItem>
     * @date: 2019/11/7
     */
    @GetMapping("/findCheckGroudAndCheckItemInfo")
    public Result findCheckGroudAndCheckItemInfo(Integer id) {
        Map<String, Object> map= null;
        try {
             map= checkGroupService.findCheckGroudAndCheckItemInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, MessageConstant.EXCEPION);
        }
        if (map == null) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, MessageConstant.EXCEPION);
        }

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, map);
    }

 /*   *//**
     * 根据检查组的id查询检查项的相应id
     * url:/checkgroup/findCheckItemIdsByCheckGroupId.do?id
     *
     * @param:
     * @return: null
     * @date: 2019/11/8
     *//*
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {

        List<Integer> checkItemIdsByCheckGroupId = null;
        try {
            checkItemIdsByCheckGroupId = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, "服务器正忙....");
        }
        if (checkItemIdsByCheckGroupId == null) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL, "服务器正忙....");

        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkItemIdsByCheckGroupId);
    }
*/
    /**
     * url:/checkgroup/edit.do?checkitemIds=" + this.checkitemIds,this.formData
     * 编辑
     *
     * @param:
     * @return: null
     * @date: 2019/11/9
     */
    @PostMapping("/edit")
    public Result edit(int[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        int edit = 0;
        try {
            edit = checkGroupService.edit(checkitemIds, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL, e.getMessage());
        }
        if (edit == 0) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL, MessageConstant.EXCEPION);
        }

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据id进行删除
     *
     * @param:id
     * @return: int
     * @date: 2019/11/7
     */
    @GetMapping("/delete")
    public Result delete(Integer id) {
        int deleteById;
        try {
            deleteById = checkGroupService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL, e.getMessage());
        }
        if (deleteById == 0) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL, MessageConstant.EXCEPION);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
    /**
     * 查询所有检查组
     * url:/checkitem/findAll.do
     *
     * @param:null
     * @return:List<CheckItem>
     * @date: 2019/11/8
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = null;
        try {
            checkGroupList = checkGroupService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.EXCEPION);
        }
        if (checkGroupList == null) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.EXCEPION);

        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkGroupList);
    }

}
